package com.j3.xml;

import java.io.FileWriter;

import org.dom4j.io.DOMReader;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// import org.dom4j.dom.DOMDocument;
public class XMLDocUtils {

	public static Node getFirstChildByName(Node node, String name) {
		if (!node.hasChildNodes()) {
			return null;
		}
		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node child = list.item(i);
			if (name.equals(child.getNodeName())) {
				return child;
			}
		}
		return null;
	}

	/**
	 *
	 * 描述：删除某个节点下面的所有节点
	 *
	 * @param node
	 */
	public static void removeAllChildren(Node node) {
		if (!node.hasChildNodes()) {
			return;
		}
		NodeList list = node.getChildNodes();
		for (int i = list.getLength() - 1; i >= 0; i--) {
			node.removeChild(list.item(i));
		}
		return;
	}

	/*
	 * 描述：根据属性获得节点
	 *
	 * 参数：document XML文档，tagName 标签名字，attributeName属性名字，attributeValue 属性值
	 *
	 * 返回：Node
	 */
	public static Node findNodeByAttribute(org.w3c.dom.Document document, String tagName, String attributeName, String attributeValue) {
		NodeList nodes = document.getElementsByTagName(tagName);
		if (nodes == null) {
			return null;
		}
		for (int i = 0; i < nodes.getLength(); i++) {
			NamedNodeMap attributes = nodes.item(i).getAttributes();
			if (attributes == null) {
				continue;
			}
			for (int t = 0; t < attributes.getLength(); t++) {
				Node attri = attributes.item(t);
				if (attri == null) {
					continue;
				}
				if (!attributeName.equals(attri.getNodeName())) {
					continue;
				}
				if (attributeValue.equals(attri.getNodeValue())) {
					// System.out.println(attributeValue);
					return nodes.item(i);
				}
				continue;
			}
		}
		return null;
	}

	/*
	 * 描述：获得节点的属性值
	 *
	 * 参数：node XML节点，attributeName 属性名称
	 *
	 * 返回：String
	 */
	public static String getNodeAttribute(Node node, String attributeName) {
		NamedNodeMap map = node.getAttributes();
		Node temp = map.getNamedItem(attributeName);
		if (temp == null) {
			return null;
		}
		return temp.getNodeValue();
	}

	/*
	 * 描述：获得节点的属性值
	 *
	 * 参数：node XML节点，attributeName 属性名称
	 *
	 * 返回：int
	 */
	public static int getNodeIntAttribute(Node node, String attributeName) {
		String attr = getNodeAttribute(node, attributeName);
		if (attr == null) {
			return 0;
		}
		return Integer.parseInt(attr);
	}

	public static boolean getNodeBooleanAttribute(Node node, String attributeName) {
		String attr = getNodeAttribute(node, attributeName);
		if (attr == null) {
			return false;
		}
		return Boolean.parseBoolean(attr);
	}

	public static long getNodeLongAttribute(Node node, String attributeName) {
		String attr = getNodeAttribute(node, attributeName);
		if (attr == null) {
			return 0;
		}
		return Long.parseLong(attr);
	}

	public static double getNodeDoubleAttribute(Node node, String attributeName) {
		String attr = getNodeAttribute(node, attributeName);
		if (attr == null) {
			return 0;
		}
		return Double.parseDouble(attr);
	}

	public static void writeXmlFile(org.dom4j.Document document, String file, String encoding) {
		// System.out.println(document);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding(encoding);
		try {
			FileWriter fwriter = new FileWriter(file);
			XMLWriter xmlWriter = new XMLWriter(fwriter, format);
			xmlWriter.write(document);
			xmlWriter.close();
			fwriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * ��ʽ��������xml�ĵ�
	 *
	 * @param document
	 * @param file
	 * @param encoding
	 */
	public static void writeXmlFile(org.w3c.dom.Document document, String file, String encoding) {
		DOMReader reader = new DOMReader();
		org.dom4j.Document d = reader.read(document);
		writeXmlFile(d, file, encoding);
	}
}
